package com.ctrl.riveradministrator.mvp.view.fragment;


import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.model.LatLng;
import com.ctrl.riveradministrator.R;
import com.ctrl.riveradministrator.base.BaseFragment;
import com.ctrl.riveradministrator.base.BasePresenter;
import com.ctrl.riveradministrator.RaApplication;
import com.ctrl.riveradministrator.MainActivity;
import com.ctrl.riveradministrator.util.BitmapUtil;
import com.ctrl.riveradministrator.util.MapUtil;

public class RiverFragment extends BaseFragment implements BaiduMap.OnMarkerClickListener {

    public static RiverFragment INSTANCE;

    private Toolbar toolbar;
    private TextView titleTv;
    private ImageView leftMenu;
    private DrawerLayout drawerLayout;
    private MapView mapView;
    private BaiduMap map;
    private MapUtil mapUtil;

    private LocationClient locationClient;
    private LocationListener locationListener;
    private MyLocationConfiguration.LocationMode locationMode;

    public static RiverFragment newInstance() {
        if (INSTANCE == null) {
            INSTANCE = new RiverFragment();
        }
        return INSTANCE;
    }

    public RiverFragment() {

    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
        map.setMyLocationEnabled(false);
        locationClient.stop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    @Override
    protected int bindLayout() {
        return R.layout.fragment_river;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void initView(View view) {
        toolbar = (Toolbar) $(view, R.id.toolbar);
        titleTv = (TextView) $(view, R.id.title_tv);
        leftMenu = (ImageView) $(view, R.id.left_iv);
        mapView = (MapView) $(view, R.id.map);
        //drawerLayout = getActivity().findViewById(R.id.drawer);
        map = mapView.getMap();
        MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(17.0f);
        map.setMapStatus(msu);
        try {
            initLocation();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        mapUtil = MapUtil.getInstance();
        BitmapUtil.init();
        mapUtil.init(mapView, getContext());
        mapUtil.setCenter();
        /*
         * 设置toolbar
         * */
        toolbar.setTitleTextColor(Color.WHITE);
        toolbar.setTitle("");
        titleTv.setText(getResources().getString(R.string.river_patrol));
        leftMenu.setBackground(getContext().getDrawable(R.drawable.ic_menu_24dp));
        ((MainActivity) getActivity()).setSupportActionBar(toolbar);
    }

    @Override
    protected void doBusiness(Context mContext, Activity activity) {
    }


    @Override
    protected void widgetClick(View view) {
        switch (view.getId()) {
            case R.id.left_iv:
                drawerLayout.openDrawer(GravityCompat.START);
                break;
        }
    }

    @Override
    protected void setListener() {
        leftMenu.setOnClickListener(this);
    }

    @Override
    public void handleMessage(Message message, int what) {

    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }


    private void initLocation() throws Exception {
        locationClient = new LocationClient(RaApplication.raApplication);
        LocationClientOption locationOption = new LocationClientOption();
        LocationClient.setAgreePrivacy(true);
        locationListener = new LocationListener();
        locationClient.registerLocationListener(locationListener);
        locationOption.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        //可选，默认gcj02，设置返回的定位结果坐标系，如果配合百度地图使用，建议设置为bd09ll;
        locationOption.setCoorType("bd09ll");
        //可选，默认0，即仅定位一次，设置发起连续定位请求的间隔需要大于等于1000ms才是有效的
        locationOption.setScanSpan(1000);
        //可选，设置是否需要地址信息，默认不需要
        locationOption.setIsNeedAddress(true);
        //可选，设置是否需要地址描述
        locationOption.setIsNeedLocationDescribe(true);
        //可选，设置是否需要设备方向结果
        locationOption.setNeedDeviceDirect(false);
        //可选，默认false，设置是否当gps有效时按照1S1次频率输出GPS结果
        locationOption.setLocationNotify(true);
        //可选，默认true，定位SDK内部是一个SERVICE，并放到了独立进程，设置是否在stop的时候杀死这个进程，默认不杀死
        locationOption.setIgnoreKillProcess(true);
        //可选，默认false，设置是否需要位置语义化结果，可以在BDLocation.getLocationDescribe里得到，结果类似于“在北京天安门附近”
        locationOption.setIsNeedLocationDescribe(true);
        //可选，默认false，设置是否需要POI结果，可以在BDLocation.getPoiList里得到
        locationOption.setIsNeedLocationPoiList(true);
        //可选，默认false，设置是否收集CRASH信息，默认收集
        locationOption.SetIgnoreCacheException(false);
        //可选，默认false，设置是否开启Gps定位
        locationOption.setOpenGps(true);
        //可选，默认false，设置定位时是否需要海拔信息，默认不需要，除基础定位版本都可用
        locationOption.setIsNeedAltitude(false);
        //设置打开自动回调位置模式，该开关打开后，期间只要定位SDK检测到位置变化就会主动回调给开发者，该模式下开发者无需再关心定位间隔是多少，定位SDK本身发现位置变化就会及时回调给开发者
        locationOption.setOpenAutoNotifyMode();
        //设置打开自动回调位置模式，该开关打开后，期间只要定位SDK检测到位置变化就会主动回调给开发者
        locationOption.setOpenAutoNotifyMode(3000, 1, LocationClientOption.LOC_SENSITIVITY_HIGHT);
        //需将配置好的LocationClientOption对象，通过setLocOption方法传递给LocationClient对象使用
        locationClient.setLocOption(locationOption);
        //开始定位
        locationClient.start();

    }

    @Override
    public boolean onMarkerClick(Marker marker) {
        return false;
    }


    private class LocationListener extends BDAbstractLocationListener {

        @Override
        public void onReceiveLocation(BDLocation bdLocation) {
            if (bdLocation == null || mapView == null) {
                return;
            }
            Log.e(TAG, "onReceiveLocation: " + bdLocation.getLatitude() + ", " + bdLocation.getLongitude() + ", " + bdLocation.getAddress());
            LatLng latLng = new LatLng(bdLocation.getLatitude(), bdLocation.getLongitude());
            MapStatusUpdate update = MapStatusUpdateFactory.newLatLng(latLng);
            map.animateMapStatus(update);
            update = MapStatusUpdateFactory.zoomTo(16f);
            map.animateMapStatus(update);
        }
    }
}
